<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sepetim_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    public function get_cart_id_by_customer_id($customer_id)
    {
        $result = $this->db->where(array('customer_id'=>$customer_id, 'status'=>1))->get('cart');
        return $result;         
    }
    public function get_cart_id_by_session_id($session_id)
    {

        $result = $this->db->where(array('cisession'=>$session_id, 'status'=>1))->get('cart');
        return $result;          
    }
    public function check_stocks($cart_id)
    {
        $products = $this->db->where('cart_id',$cart_id)->get('cart_products');
        if($products->num_rows() > 0 )
        {
            foreach($products->result() as $product )
            {
                $seller_id = $product->seller_id;
                $quantitiy = $product->quantitiy;
                $seller_info = $this->db->where(array('seller_id' => $seller_id, 'status' => 1 ))->get('seller');
                if($seller_info->num_rows()> 0)
                {
                    $stock = $seller_info->row(0)->stock;
                    if($stock < $quantitiy)
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }    
        }
        else
        {
            return false;
        }
        return true;
        
    }
    public function fill_cart_by_customer_id($user_id)
    {
        if($this->get_cart_id_by_customer_id($user_id)->num_rows()> 0)
        {
            $cart_id = $this->get_cart_id_by_customer_id($user_id)->row(0)->cart_id;
            if($this->db->where('cart_id',$cart_id)->get('cart_products')->num_rows()>0)
            {
                
                
                $items = $this->db->where('cart_id',$cart_id)->get('cart_products')->num_rows();
                $info['items'] = $items;
                $total = 0; 
                foreach($this->db->where('cart_id',$cart_id)->get('cart_products')->result() as $product)
                {
                    $seller_id = $product->seller_id;
                    
                 // $fiyat_aralik_id = $this->db->where(array('seller_id' => $seller_id))->get('seller')->row(0)->fiyat_araligi_id;
                 /* if($fiyat_aralik_id != "0")
                  {
                     $fiyat_araligi = $this->db->where('fiyat_araligi_id',$fiyat_aralik_id)->get('fiyat_araligi')->row(0);
                    
                      if($product->quantitiy > $fiyat_araligi->aralik12  )
                      {
                         $unit_price =  $fiyat_araligi->fiyat2;
                        // $pro['price'] = $fiyat_araligi->fiyat2;
                      } 
                      else
                      {
                        $unit_price =  $fiyat_araligi->fiyat1;
                      } 
                  }*/
                  
                  
                    
                   
                    
                    
                 
                    
                    
                    $product_detail = $this->db->where('product_id',$product->product_id)->get('products')->row(0);
                    $unit_price = $product_detail->standart_price; 
                    $product_info['image'] =  $product_detail->image1;
                    $product_info['ptotal'] =  $product->quantitiy * $unit_price; 
                    $product_info['name']   = $product_detail->name;
                    $product_info['model'] = $product_detail->model;
                    $total += $unit_price * $product->quantitiy;
                     $product_info['product_id'] = $product->product_id;
                     $product_info['quantitiy'] = $product->quantitiy;
                     $product_info['price'] = $unit_price;
                     $product_info['color_id'] = $product->color_id;
                     $product_info['size_id'] = $product->size_id;
                     $pros[] = $product_info;
                    
                }
                
                $info['cart_id'] = $cart_id;
                $info['products'] = $pros;
                $info['total'] = $total;
                return $info;
            }
            else
            {
                return NULL;
            }
        }
        else
        {
            return NULL;
        }
    }
    public function fill_cart_by_session_id($session_id)
    {
       if($this->get_cart_id_by_session_id($session_id)->num_rows()> 0)
        {
            $cart_id = $this->get_cart_id_by_session_id($session_id)->row(0)->cart_id;
            if($this->db->where('cart_id',$cart_id)->get('cart_products')->num_rows()>0)
            {
                

                   $items = $this->db->where('cart_id',$cart_id)->get('cart_products')->num_rows();
                $info['items'] = $items;
                $total = 0; 
                foreach($this->db->where('cart_id',$cart_id)->get('cart_products')->result() as $product)
                {
                 $seller_id = $product->seller_id;
                    
                  //$fiyat_aralik_id = $this->db->where(array('seller_id' => $seller_id))->get('seller')->row(0)->fiyat_araligi_id;
                /*  if($fiyat_aralik_id != "0")
                  {
                     $fiyat_araligi = $this->db->where('fiyat_araligi_id',$fiyat_aralik_id)->get('fiyat_araligi')->row(0);
                    
                      if($product->quantitiy > $fiyat_araligi->aralik12  )
                      {
                         $unit_price =  $fiyat_araligi->fiyat2;
                  
                      }
                      else
                      {
                        $unit_price =  $fiyat_araligi->fiyat1;
                      } 
                  }*/
                 
                      
                  
                    
                    
                    $product_detail = $this->db->where('product_id',$product->product_id)->get('products')->row(0);
                    $unit_price = $product_detail->standart_price;
                    $product_info['image'] =  $product_detail->image1;
                    $product_info['ptotal'] =  $product->quantitiy * $unit_price; 
                   $product_info['name']   = $product_detail->name;
                    $product_info['model'] = $product_detail->model;
                    $total += $unit_price* $product->quantitiy;
                     $product_info['product_id'] = $product->product_id;
                     $product_info['quantitiy'] = $product->quantitiy;
                     $product_info['price'] = $unit_price;
                     $product_info['color_id'] = $product->color_id;
                     $product_info['size_id'] = $product->size_id;
                     $pros[] = $product_info;
                    
                }
                
                $info['cart_id'] = $cart_id;
                $info['products'] = $pros;
                $info['total'] = $total;
                return $info;
            }
            else
            {
                return NULL;
            }
        }
        else
        {
            return NULL;
        }        
        
    }
    public function get_cart_products($cart_id)
    {
        
            if($this->db->where('cart_id',$cart_id)->get('cart_products')->num_rows()>0)
            {
                
                
                $items = $this->db->where('cart_id',$cart_id)->get('cart_products')->num_rows();
                $info['items'] = $items;
                $total = 0; 
                foreach($this->db->where('cart_id',$cart_id)->get('cart_products')->result() as $product)
                {
                    $seller_id = $product->seller_id;
                    
               //   $fiyat_aralik_id = $this->db->where(array('seller_id' => $seller_id))->get('seller')->row(0)->fiyat_araligi_id;
                 /* if($fiyat_aralik_id != "0")
                  {
                     $fiyat_araligi = $this->db->where('fiyat_araligi_id',$fiyat_aralik_id)->get('fiyat_araligi')->row(0);
                    
                      if($product->quantitiy > $fiyat_araligi->aralik12  )
                      {
                         $unit_price =  $fiyat_araligi->fiyat2;
                        // $pro['price'] = $fiyat_araligi->fiyat2;
                      } 
                      else
                      {
                        $unit_price =  $fiyat_araligi->fiyat1;
                      } 
                  }*/
                 
                   
                    
                    
                 
                    
                     $unit_price = $product->standart_price;
                    $product_detail = $this->db->where('product_id',$product->product_id)->get('products')->row(0);
                    $product_info['image'] =  $product_detail->image1;
                    $product_info['ptotal'] =  $product->quantitiy * $unit_price; 
                    $product_info['name']   = $product_detail->name;
                    $product_info['model'] = $product_detail->model;
                    $total += $unit_price * $product->quantitiy;
                     $product_info['product_id'] = $product->product_id;
                     $product_info['quantitiy'] = $product->quantitiy;
                     $product_info['price'] = $unit_price;
                     $product_info['color_id'] = $product->color_id;
                     $product_info['size_id'] = $product->size_id;
                     $pros[] = $product_info;
                    
                }
                
                $info['cart_id'] = $cart_id;
                $info['products'] = $pros;
                $info['total'] = $total;
                return $info;
            }
            else
            {
                return false;
            }
    }
  



}
?>